import java.util.*;

public class Test2 {
    // 存在重复元素 II
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                int j = map.get(nums[i]);
                if (i - j <= k) {
                    return true;
                }
            }
            map.put(nums[i],i);
        }
        return false;
    }


    // 存在重复元素
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int x : nums) {
            if (set.contains(x)) {
                return true;
            }
            set.add(x);
        }
        return false;
    }


    // 前K个高频单词
    public List<String> topKFrequent(String[] words, int k) {
        Map<String,Integer> hashMap = new HashMap<>();
        // 1. 遍历数组 统计每个单词出现的次数
        for (String s : words) {
            hashMap.put(s, hashMap.getOrDefault(s,0) + 1);
        }
        // 2. 建立小根堆
        // 传入大小k 和 比较器
        CompareSI compareSI = new CompareSI();
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(k, compareSI);
        // 3. 遍历hashMap 把里面的数据放到小根堆
        for (Map.Entry<String,Integer> entry : hashMap.entrySet()) {
            if (minHeap.size() < k) {
                minHeap.offer(entry);
            } else {
                // 小根堆放满了k个
                // entry与堆顶元素比较
                Map.Entry<String,Integer> top = minHeap.peek();
                if (compareSI.compare(entry, top) > 0) {
                    minHeap.poll();
                    minHeap.offer(entry);
                }
            }
        }
        // 4. 此时minHeap中已经存了最高频的10个单词
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            // 弹出堆顶元素 放入 ret
            ret.add(minHeap.poll().getKey());
        }
        // 5. 反转ret
        Collections.reverse(ret);
        return ret;
    }

    // 比较器
    // 先比较Integer 再比较 String
    // Integer 要大的
    // String 要小的
    public class CompareSI implements Comparator<Map.Entry<String,Integer>> {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            int v = o1.getValue().compareTo(o2.getValue());
            if (v != 0) {
                return v;
            }
            // value相同 用比较key
            return o2.getKey().compareTo(o1.getKey());
        }
    }




    public static void func(String str1, String str2) {
        Set<Character> set1 = new HashSet();
        for(char x : str2.toUpperCase().toCharArray()) {
            set1.add(x);
        }

        Set<Character> set2 = new HashSet();
        for(char x : str1.toUpperCase().toCharArray()) {
            if (!set1.contains(x) && !set2.contains(x)) {
                set2.add(x);
                System.out.print(x);
            }
        }
    }
    public static void main4(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()) {
            String str1 = scan.nextLine();
            String str2 = scan.nextLine();
            func(str1,str2);
        }
    }




    // 宝石与石头
    public int numJewelsInStones(String jewels, String stones) {
        Set<Character> set = new HashSet();
        int count = 0;

        for (int i = 0; i < jewels.length(); i++) {
            set.add(jewels.charAt(i));
        }

        for (int i = 0; i < stones.length(); i++) {
            if (set.contains(stones.charAt(i))) {
                count++;
            }
        }
        return count;
    }



    // 复制带随机指针的链表
    /*
    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            Node node = new Node(cur.val);
            map.put(cur, node);
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
    */



    // 只出现一次的数字
    public int singleNumber(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int x : nums) {
            if(!set.contains(x)) {
                set.add(x);
            }else {
                set.remove(x);
            }
        }
        for(int x : nums) {
            if (set.contains(x)) {
                return x;
            }
        }
        return -1;
    }



    // 找到重复的数
    public static void main3(String[] args) {
        int[] array = {1,2,2,3,4,5,6,8,4};
        Map<Integer,Integer> map = new HashMap<>();
        for (int x : array) {
            // 将数组中的值当作key放入 map
            // value 统计次数
            if(map.get(x) == null) {
                map.put(x,1);
            }else {
                int val = map.get(x);
                map.put(x,val+1);
            }
        }
        for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
            if(entry.getValue() > 1) {
                System.out.println("key: "+entry.getKey()+" val: "+entry.getValue());
            }
        }
    }



    // 找到第一个重复的数
    public static void main2(String[] args) {
        int[] array = {1,2,2,3,4,5,6,8,4};
        Set<Integer> set = new HashSet<>();
        for (int x : array) {
            if(!set.contains(x)) {
                set.add(x);
            }else {
                System.out.println(x);
                return;
            }
        }
    }



    // 去重
    public static void main1(String[] args) {
        int[] array = {1,2,2,3,4,5,6,8,4};
        Set<Integer> set = new HashSet<>();
        for (int x : array) {
            set.add(x);
        }
        System.out.println(set);
    }
}
